package com.taobaolite.search;

import com.taobaolite.item.Item;
import com.taobaolite.item.ItemBody;
import com.taobaolite.item.ItemRepository;
import com.taobaolite.user.store.StoreBody;
import com.taobaolite.user.store.StoreRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SearchService {

    private final ItemRepository itemRepo;
    private final StoreRepository storeRepo;

    @Autowired
    public SearchService(ItemRepository itemRepo, StoreRepository storeRepo) {
        this.itemRepo = itemRepo;
        this.storeRepo = storeRepo;
    }

    public List<ItemBody> searchItems(String item_q) {
        List<Item> itemList = itemRepo.searchByName(item_q);

//        List<ItemBody> itemBodyList = new ArrayList<>();
//
//        for (Item item : itemList) {
//            itemBodyList.add(new ItemBody(item));
//        }
//        return itemBodyList;

        return itemList.stream().map(ItemBody::new).collect(Collectors.toList());
    }

    public List<StoreBody> searchStores(String store_q) {
        return storeRepo.searchByName(store_q).stream().map(StoreBody::new).collect(Collectors.toList());
    }
}
